<template>
  <div class="shareList">
    <navBar title="面经分享" path="/home/find"></navBar>
    <div class="content">
      <van-search
        v-model="inputValue"
        show-action
        input-align="center"
        shape="round"
        placeholder="请输入关键字"
        @search="search"
        @cancel="cancel"
        class="search"
      ></van-search>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="别看了~我是有底限的"
        @load="load"
        v-if="!bol"
      >
        <articles
          v-for="(item, index) in shareList"
          :key="index"
          :info="item"
        ></articles>
        <div class="noData" v-if="finished && shareList.length === 0">
          别看啦~没有了 去看看别的吧
        </div>
      </van-list>

      <div class="history" v-else>
        <h3 class="title">大家都在搜</h3>
        <van-tag
          class="tag"
          color="#f7f4f5"
          text-color="#545671"
          v-for="(item, index) in hotList"
          :key="index"
          @click="search(item)"
          >{{ item }}</van-tag
        >

        <div class="historyList">
          <h3 class="title">历史搜索</h3>
          <span class="clear" @click="clear">清空</span>
        </div>
        <van-tag
          class="tag"
          color="#f7f4f5"
          text-color="#545671"
          v-for="(item, index) in historyList"
          :key="index"
          @click="search(item)"
          >{{ item }}</van-tag
        >
      </div>
    </div>
  </div>
</template>

<script>
import articles from "./articles";
import { articlesShare, ShareTopSearch } from "@/api/find.js";
import { saveLocal,getLocal,removeLocal } from "@/utils/local.js";
export default {
  components: {
    articles,
  },
  data() {
    return {
      hotList: [],
      historyList: JSON.parse(getLocal("history")) || [], // 字符串转数组  parse
      bol: false,
      inputValue: "",
      shareList: [],
      loading: false,
      finished: false,
      currentPage: 0, // 页码
      pageSize: 1, // 页容量
    };
  },
  created() {
    ShareTopSearch().then((res) => {
      window.console.log("热搜", res);
      this.hotList = res.data.data;
    });
  },
  methods: {
    search(value) {
      this.bol = false;
      this.inputValue = value;
      this.historyList.unshift(value);
      this.historyList = [...new Set(this.historyList)]; //去重
      saveLocal(JSON.stringify(this.historyList), "history"); // 数组转成字符串  styingify 
      this.currentPage = 0;
      this.shareList = [];
      this.finished = false;
      this.loading = true;
      this.$nextTick(() => {
        this.loading = false;
      });
    },
    cancel() {
      this.bol = true;
    },
    load() {
      this.currentPage++;
      const _q = {
        start: (this.currentPage - 1) * this.pageSize,
        limit: this.pageSize,
        q: this.inputValue,
      };
      articlesShare(_q).then((res) => {
        res.data.data.list.forEach((item) => {
          if(this.inputValue != ''){
            const _arr = item.title.split(this.inputValue)
            item.title = _arr.join(
              "<span style='color:red'>" + this.inputValue + "</span>"
            )
          }
        });
        this.shareList.push(...res.data.data.list);
        this.loading = false;
        if (this.shareList.length >= res.data.data.total) {
          this.finished = true;
        }
        window.console.log("面经技巧", res);
      });
    },
    clear(){
      this.historyList = []
      removeLocal('history')
    }
  },
};
</script>

<style lang="less" scoped>
.shareList {
  .content {
    padding: 0 @p15;
    .search {
      height: 34px;
      background: #fff;
    }
    .noData {
      color: #76eac5;
      margin: 100px auto 0;
      text-align: center;
    }
    .history {
      margin-top: 12px;
      .title {
        margin-top: 21px;
        margin-bottom: 18px;
        font-size: 18px;
        font-family: PingFangSC, PingFangSC-Medium;
        font-weight: 500;
        text-align: left;
        color: #222222;
        line-height: 25px;
        letter-spacing: 0px;
      }
      .tag {
        height: 32px;
        border-radius: 5px;
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 500;
        text-align: left;
        line-height: 16px;
        letter-spacing: 0px;
        margin-right: 15px;
        padding: 8px 11px;
      }
      .historyList {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .clear {
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: left;
          color: #545671;
          line-height: 16px;
          letter-spacing: 0px;
        }
      }
    }
  }
}
</style>